﻿/// <reference path="../core/jquery-1.4.2.min-vsdoc.js"/>
/// <reference path="../config/sw-config-default.js"/>
/// <reference path="../core/sw-prototype.js"/>
/// <reference path="../core/sw.js"/>
/// <reference path="../core/sw-control-core.js"/>

/***************************************************************************
jQuery tooltip
NOTE: Created for SmartUI. 
Copyright 2010, Roy zhang
Features：
1.实现属性内容、固定内容、目标内容、ajax内容,tooltip
2.
3.
4.
Update Note：

Needs：
****************************************************************************/

(function ($) {
    ///#region 设置
    var tooltipSetting = {
        id: '_tip'
        , cssarrow: 'tiparrow'
    };
    ///#endregion

    $.fn.tooltip = function (options) {
        /// <summary>pager</summary>
        /// <param name="options" type="Array">配置项</param>
        /// <param name="datas" type="Json">数据源</param>               
        options = $.extend({
            cssname: 'tooltip'
            , csscontent: 'tipcontent'
            , csstipctr: 'ctrtip'
            , tipprop: 'title'
            , content: null
            , activation: "hover"                //触发的事件类型hover、click
        }, options);
        options.css = $.extend({
            zindex: iconfig.tooltip.css.zindex
                , opacity: iconfig.tooltip.css.opacity
                , width: iconfig.tooltip.css.width
                , height: iconfig.tooltip.css.height
                , left: iconfig.tooltip.css.left           //对应x轴的偏移量
                , top: iconfig.tooltip.css.top           //对象y轴的偏移量
        }, options.css);

        this.each(function () {
            var ctr = $(this).addClass(options.csstipctr), timeout = false, isclose = true, tooltip, ctrcontent,
                csstip = options.cssname, csscontent = options.csscontent;

            var id = ctr.attr("id");
            if (strNotEmpty(id)) {
                id += tooltipSetting.id;
                tooltip = ctr.parent().find('#' + id);
            }
            else
                id = null;
            if (!tooltip || !tooltip.length) {
                tooltip = $(document.createElement('div')).addClass(csstip).appendTo(ctr.parent())
                                    .hover(function () { isclose = false; }, function () { isclose = true; tooltipClose(tooltip) });
                if (id)
                    tooltip.attr('id', id);
                if (options.activation === "hover")
                    ctr.hover(function () { tooltipShow($(this), tooltip, options.css); }, function () { tooltipClose(tooltip); });
                else if (options.activation === "click")
                    ctr.click(function () { tooltipShow($(this), tooltip, options.css); });
                var ctrarrow = $(document.createElement('div')).addClass(tooltipSetting.cssarrow).appendTo(tooltip);

                ctrcontent = $(document.createElement('div')).width(options.css.width - 35).addClass(options.csscontent).appendTo(tooltip);
            }
            else
                ctrcontent = tooltip.children('.' + csscontent);

            if (options.content)//指定内容
                ctrcontent.html(options.content);
            else {//Title内容
                var tipcontent = ctr.attr(options.tipprop);
                if (strNotEmpty(tipcontent)) {
                    ctrcontent.html(tipcontent);
                    ctr.removeAttr(options.tipprop);
                }
                else {//目标内容
                    var tipid = ctr.attr('tipid');
                    if (strNotEmpty(tipid)) {
                        $('#' + tipid).css('display', '').appendTo(ctrcontent);
                        ctr.removeAttr('tipid');
                    }
                    else if (strNotEmpty(ctr.attr('url')))
                        tooltip.data('isajax', 1);
                }
            }
            ///#region 公共设置方法
            function tooltipShow(ctr, tooltip, css) {
                if (tooltip.data('isajax')) {
                    var ctrcontent = ctrcontent = tooltip.children('.' + csscontent);
                    var url = ctr.attr('url');
                    if (strNotEmpty(url))
                        ictrcore.loadPage(url, ctrcontent, ctr.attr('params'));
                    tooltip.removeData('isajax');
                }
                ictrcore.upTip(ctr, tooltip, css, false);
                if (timeout) { clearTimeout(timeout); }
                timeout = setTimeout(function () { tooltip.stop(true, true).fadeIn('normal'); }, 100);
            }
            function tooltipClose(tooltip) {
                if (timeout) { clearTimeout(timeout); }
                timeout = setTimeout(function () { if (isclose) tooltip.fadeOut('fast'); }, 100);
            }
            ///#endregion
        });
    };
})(jQuery);